{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import os.path\n",
    "import re\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Parses a folder with Spirent test results and shows an extract of each result. Useful for comparing (e.g. test results from a series of tests)\n",
    "folder = 'spirent tests'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_list = []\n",
    "for path in os.listdir(folder):\n",
    "    spirent_file = os.path.join(folder,path)\n",
    "    # e.g. 21-02-09_08.31.53__RID-123__Test_name.xls\n",
    "    match = re.search(r'(\\d\\d-\\d\\d-\\d\\d_\\d\\d.\\d\\d.\\d\\d)__RID-(\\d\\d\\d)__(.*).xls', path)\n",
    "    if match is None:\n",
    "        continue\n",
    "    try:\n",
    "        df = pd.read_excel(spirent_file, sheet_name='L5-7 Client|Basic')\n",
    "    except:\n",
    "        print('Could not read L5-7 Client|Basic sheet from {0}'.format(path))\n",
    "        continue\n",
    "    df['file'] = path\n",
    "    df['datetime'] = datetime.datetime.strptime(match.group(1), '%y-%m-%d_%H.%M.%S')\n",
    "    df['test number'] = match.group(2)\n",
    "    df['test name'] = match.group(3)\n",
    "    \n",
    "    mm_df = pd.read_excel(spirent_file, sheet_name='MM')\n",
    "    sm_df = pd.read_excel(spirent_file, sheet_name='SM')\n",
    "    scf_sut_info = pd.read_excel(spirent_file, sheet_name='CFG-SUT INFO', skiprows=2)\n",
    "    \n",
    "    df['sut name'] = scf_sut_info.iloc[0,0]\n",
    "    df['sut ip'] = scf_sut_info.iloc[0,1]\n",
    "    df['packet loss (%)'] = df['Basic Data Messages Lost'] / df['Basic Data Messages Sent'] * 100\n",
    "    \n",
    "    df = df.join(mm_df.iloc[:,3:])\n",
    "    df = df.join(sm_df.iloc[:,3:])\n",
    "    df_list.append(df)\n",
    "df = pd.concat(df_list, ignore_index=True)\n",
    "df = df.sort_values(by='datetime')\n",
    "df['Basic Data Messages Lost (calculated)'] = df['Basic Data Messages Sent'] - df['Basic Data Messages Received']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Case where there is a mix of SUTs\n",
    "# filtered_df = df[df['sut name']!='XXX']\n",
    "filtered_df = df\n",
    "summary_table = filtered_df[filtered_df['Interval']=='Current'].loc[:,[\n",
    "    'test number', 'test name', 'datetime', \n",
    "    'Registration Requests', 'Registration Completes', 'Pdu Establishment Requests', 'Pdu Establishment Accepts', \n",
    "    'Basic Data Messages Sent', 'Basic Data Messages Received', 'Basic Data Messages Lost', 'Basic Data Messages Lost (calculated)', 'packet loss (%)',\n",
    "    'Basic Data Message One Way Trip Delay (micro-sec)', 'Basic Data Minimum One Way Delay (micro-sec)', 'Basic Data Maximum One Way Delay (micro-sec)', 'sut ip']]\n",
    "\n",
    "display(summary_table)\n",
    "summary_table.to_clipboard(excel=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly.express as px\n",
    "fig = px.bar(summary_table, x='test number', y='Basic Data Message One Way Trip Delay (micro-sec)')\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
